import requests
import time
import _json


class Image(object):

    def __init__(self):
        self.url = 'https://image.baidu.com/search/acjson?'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36',
            'Cookie': 'BDqhfp=%E7%8B%97%E6%90%9E%E7%AC%91%26%260-10-1undefined%26%264179%26%265; BAIDUID=8496E1C1C69CEA932C5C32C1D58C3CB2:FG=1; BIDUPSID=8496E1C1C69CEA932C5C32C1D58C3CB2; PSTM=1613739276; BDUSS=XUyNnBlc3dJcmxITjBEWGxRMUg1OX5ESjdpdnJtYmc4LVUxSGRVR081LTVZbGhnRVFBQUFBJCQAAAAAAAAAAAEAAADOzmtHNTXOyM7ItbG1sQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALnVMGC51TBge; __yjs_duid=1_0c997cff8be268ec61c34a688602997d1619022974639; BAIDU_WISE_UID=wapp_1629904068584_105; BDRCVFR[mKMaR01Dvmb]=o5h-9ZptSamXZ7Bmy4bQhP8; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; firstShowTip=1; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; ZD_ENTRY=baidu; indexPageSugList=%5B%22%E7%88%AC%E8%99%AB%E5%87%BA%E7%8E%B0KeyError%3A%20'

        }
        self.params = {'tn': 'resultjson_com',
                       'logid': '11387958099968100847',
                       'ipn': 'rj',
                       'ct': '201326592',
                       'is': '',
                       'fp': 'result',
                       'queryWord': '狗',
                       'cl': '2',
                       'lm': '-1',
                       'ie': 'utf-8',
                       'oe': 'utf-8',
                       'adpicid': '',
                       'st': '-1',
                       'z': '',
                       'ic': '0',
                       'hd': '',
                       'latest': '',
                       'copyright': '',
                       'word': '狗',
                       's': '',
                       'se': '',
                       'tab': '',
                       'width': '',
                       'height': '',
                       'face': '0',
                       'istype': '2',
                       'qc': '',
                       'nc': '1',
                       'fr': '',
                       'expermode': '',
                       'nojc': '',
                       'pn': '90',
                       'rn': '30',
                       'time': '',
                       'data':''
                       }
        self.Image_list = []

    def get_Image(self, num):
        for i in range(0, num):
            self.params['time'] = int(time.time() * 1000)
            self.params['pn'] = i * 30
            response = requests.get(url=self.url, headers=self.headers, params=self.params)
            for j in range(0, len(response.json()['data']) - 1):
                self.Image_list.append(response.json()['data'][j]['thumbURL'])

    def save_Image(self):
        n = 1
        for i in self.Image_list:
            image = requests.get(url=i)
            with open('./图片/{}.jpg'.format(n), 'wb') as f:
                f.write(image.content)
            n += 1


if __name__ == '__main__':
    image = Image()
    image.get_Image(3)
    image.save_Image()
